package com.lagou.stage6module1.service.impl;

import com.lagou.stage6module1.domain.Order;
import com.lagou.stage6module1.mapper.OrderMapper;
import com.lagou.stage6module1.service.OrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void cancelOrder(String orderId) {

    }

    @Override
    public void save(String orderId) {
        //保存订单到数据库
        Order order = new Order();
        Date date = new Date();
        order.setLimitTime(date);
        order.setPay(false);
        order.setStatus(true);
        order.setOrderId(orderId);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(10,Calendar.SECOND);
        Date limitTime = calendar.getTime();
        order.setLimitTime(limitTime);
        orderMapper.insert(order);
        rabbitTemplate.convertAndSend("ex.order","order",orderId);
    }

    @Override
    public void pay(String orderId) {
        Order order = orderMapper.findByOrderId(orderId);
        order.setPay(true);
        orderMapper.updateById(order);
    }
}
